from flask import Flask, render_template_string, request, redirect, url_for  # 导入Flask相关模块

app = Flask(__name__)

# 用于存储博客文章的列表，每篇文章是一个字典
posts = []

# 首页，显示所有文章
def index():
    return render_template_string('''
    <h1>简易博客系统</h1>
    <a href="/add">添加新文章</a>
    <ul>
    {% for post in posts %}
        <li><b>{{ post['title'] }}</b><br>{{ post['content'] }}</li>
    {% endfor %}
    </ul>
    ''', posts=posts)

# 添加新文章页面
def add():
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        posts.append({'title': title, 'content': content})
        return redirect(url_for('index'))
    return render_template_string('''
    <h1>添加新文章</h1>
    <form method="post">
        标题：<input name="title"><br>
        内容：<textarea name="content"></textarea><br>
        <input type="submit" value="提交">
    </form>
    <a href="/">返回首页</a>
    ''')

# 路由绑定
app.add_url_rule('/', 'index', index)
app.add_url_rule('/add', 'add', add, methods=['GET', 'POST'])

if __name__ == '__main__':
    app.run(debug=True) 